﻿/**************************************************************
 *
 * 唯一标识：56967dd2-3e85-456d-b0e8-5986d4747289
 * 命名空间：Sgr.UPMS.Controllers
 * 创建时间：2023/8/27 19:32:14
 * 机器名称：DESKTOP-S0D075D
 * 创建者：antho
 * 电子邮箱：fengqinhua2016@163.com
 * 描述：
 *
 **************************************************************/

using MediatR;
using Microsoft.AspNetCore.Mvc;
using Sgr.AspNetCore.ActionFilters.AuditLogs;
using Sgr.AspNetCore.ActionFilters.Permissions;
using Sgr.UPMS.Application.Commands.Duties;
using Sgr.UPMS.Application.Queries;
using Sgr.UPMS.Application.ViewModels;

namespace Sgr.UPMS.Controllers
{
    /// <summary>
    /// 职务管理接口
    /// </summary>
    [Route("api/v1/sgr/duties")]
    [ApiController]
    [Produces("application/json")]
    [Tags("Duty")]
    public class DutyController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly IDutyQueries _dutyQueries;

        /// <summary>
        /// 初始化职务管理控制器
        /// </summary>
        public DutyController(IMediator mediator,
            IDutyQueries dutyQueries)
        {
            _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
            _dutyQueries = dutyQueries ?? throw new ArgumentNullException(nameof(dutyQueries));
        }

        #region Command

        /// <summary>
        /// 创建职务
        /// </summary>
        /// <param name="command">创建职务的请求参数</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <remarks>
        /// 示例请求:
        ///
        ///     POST /api/v1/sgr/duties
        ///     {
        ///         "name": "技术总监",
        ///         "orderNumber": 1,
        ///         "remarks": "负责技术部门工作",
        ///         "orgId": 1
        ///     }
        /// </remarks>
        /// <response code="200">创建成功</response>
        /// <response code="400">请求参数验证失败</response>
        /// <response code="401">未经授权的访问</response>
        [CheckPermission(Permissions.CreateDuty)]
        [HttpPost]
        [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
        [AuditLogActionFilter("创建职务")]
        public async Task<ActionResult<bool>> CreateAsync([FromBody] CreateDutyCommand command, System.Threading.CancellationToken cancellationToken)
        {
            return Ok(await _mediator.Send(command, cancellationToken));
        }

        /// <summary>
        /// 更新职务信息
        /// </summary>
        /// <param name="command">更新职务的请求参数</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <remarks>
        /// 示例请求:
        ///
        ///     PUT /api/v1/sgr/duties
        ///     {
        ///         "dutyId": 1,
        ///         "name": "技术总监",
        ///         "state": "Normal",
        ///         "orderNumber": 1,
        ///         "remarks": "负责技术部门工作"
        ///     }
        /// </remarks>
        /// <response code="200">更新成功</response>
        /// <response code="400">请求参数验证失败</response>
        /// <response code="401">未经授权的访问</response>
        [CheckPermission(Permissions.UpdateDuty)]
        [HttpPut]
        [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
        [AuditLogActionFilter("修改职务")]
        public async Task<ActionResult<bool>> UpdateAsync([FromBody] UpdateDutyCommand command, System.Threading.CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(command, cancellationToken);
            return !result.IsSuccess ? this.CustomBadRequest(result.Message) : Ok(result.Data);
        }

        /// <summary>
        /// 删除职务
        /// </summary>
        /// <param name="id">职务ID</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <response code="200">删除成功</response>
        /// <response code="400">请求参数验证失败</response>
        /// <response code="401">未经授权的访问</response>
        [CheckPermission(Permissions.DeleteDuty)]
        [HttpDelete("{id:long:min(1)}")]
        [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
        [AuditLogActionFilter("删除职务")]
        public async Task<ActionResult<bool>> DeleteAsync([FromRoute] long id, System.Threading.CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(new DeleteDutyCommand { DutyId = id }, cancellationToken);
            return !result.IsSuccess ? this.CustomBadRequest(result.Message) : Ok(result.Data);
        }

        /// <summary>
        /// 更新职务状态
        /// </summary>
        /// <param name="command">更新职务状态的请求参数</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <remarks>
        /// 示例请求:
        ///
        ///     POST /api/v1/sgr/duties/status
        ///     {
        ///         "dutyId": 1,
        ///         "state": "Normal"
        ///     }
        /// </remarks>
        /// <response code="200">更新成功</response>
        /// <response code="400">请求参数验证失败</response>
        /// <response code="401">未经授权的访问</response>
        [CheckPermission(Permissions.ModifyDutyStatus)]
        [HttpPost("change-status")]
        [ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
        [AuditLogActionFilter("更新职务状态")]
        public async Task<ActionResult<bool>> UpdateStatusAsync([FromBody] ModifyDutyStatusCommand command, System.Threading.CancellationToken cancellationToken)
        {
            var result = await _mediator.Send(command, cancellationToken);
            return !result.IsSuccess ? this.CustomBadRequest(result.Message) : Ok(result.Data);
        }

        #endregion Command

        #region Query

        /// <summary>
        /// 获取职务详情
        /// </summary>
        /// <param name="id">职务ID</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <response code="200">获取成功</response>
        /// <response code="401">未经授权的访问</response>
        /// <response code="404">指定的职务不存在</response>
        [CheckPermission(Permissions.ViewDuty)]
        [HttpGet("{id:long:min(1)}")]
        [ProducesResponseType(typeof(OutDutyViewModel), StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        [AuditLogActionFilter("获取职务详情")]
        public async Task<ActionResult<OutDutyViewModel>> GetByIdAsync([FromRoute] long id, System.Threading.CancellationToken cancellationToken)
        {
            var result = await _dutyQueries.GetByIdAsync(id, cancellationToken);
            return result == null ? NotFound() : Ok(result);
        }

        /// <summary>
        /// 查询职务列表
        /// </summary>
        /// <param name="request">查询条件</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <remarks>
        /// 示例请求:
        ///
        ///     POST /api/v1/sgr/duties/search
        ///     {
        ///         "orgId": 1,
        ///         "state": "Normal"
        ///     }
        /// </remarks>
        /// <response code="200">查询成功</response>
        /// <response code="401">未经授权的访问</response>
        [CheckPermission(Permissions.ViewDuty)]
        [HttpPost("search")]
        [ProducesResponseType(typeof(IEnumerable<OutDutyViewModel>), StatusCodes.Status200OK)]
        [AuditLogActionFilter("查询职务列表")]
        public async Task<ActionResult<IEnumerable<OutDutyViewModel>>> SearchAsync([FromBody] InDutySearchModel request, System.Threading.CancellationToken cancellationToken)
        {
            return Ok(await _dutyQueries.GetListByOrgIdAsync(request, cancellationToken));
        }

        #endregion Query
    }
}